package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.bo.ChainGoodsSkuItemsBO;
import com.junzhiit.nengji.cms.service.ChainGoodsSkuService;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.ChainGoodsSkuItemsDetailVO;
import com.junzhiit.nengji.cms.web.vo.ChainGoodsSkuItemsVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.CartDAO;
import com.junzhiit.nengji.dao.ChainGoodsSkuItemsDAO;
import com.junzhiit.nengji.dao.ChainSkuContentDAO;
import com.junzhiit.nengji.model.ChainGoodsSkuItems;
import com.junzhiit.nengji.model.ChainSkuContent;
import com.junzhiit.nengji.model.enums.Status;

@Service
public class ChainGoodsSkuServiceImpl implements ChainGoodsSkuService{
	
	@Autowired
	private ChainGoodsSkuItemsDAO chainGoodsSkuItemsDAO;

	@Autowired
	private ChainSkuContentDAO chainSkuContentDAO;

	@Autowired
	private CartDAO cartDAO;

	@Value("${upload.mall.goods.sku}")
	private String skuPath;

	@Value("${upload.temp}")
	private String uploadPath;


	@Override
	public ResultVO queryGoodsSkuItems(String chainGoodsId) {
		ResultVO result = new ResultVO();
		List<ChainGoodsSkuItems> data = chainGoodsSkuItemsDAO.queryGoodsSkuItems(chainGoodsId);
		result.setData(data);
		return result;
	}

	@Override
	public Map<String, Object> findAllByGoodsId(String chainGoodsId, DataTablesParamVO dtp) {
		List<ChainSkuContent> list = chainSkuContentDAO.findAllByGoodsId(chainGoodsId, dtp.getStart(), dtp.getLength());
		int totalCount = chainSkuContentDAO.getTotalCountByGoodsId(chainGoodsId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO queryGoodsSkuItemsBO(String chainGoodsId){
		ResultVO result=new ResultVO();
		List<ChainGoodsSkuItemsBO> goodsSkuItemsBOs=chainGoodsSkuItemsDAO.queryGoodsSkuItemsBO(chainGoodsId);
		
		Map<String, ChainGoodsSkuItemsVO> map = new LinkedHashMap<String, ChainGoodsSkuItemsVO>();
		
		for (ChainGoodsSkuItemsBO bo : goodsSkuItemsBOs) {
			ChainGoodsSkuItemsVO rbo = map.get(bo.getChainSkuId());
	        if (rbo==null) {
	        	rbo = new ChainGoodsSkuItemsVO();
	        	rbo.setName(bo.getName());
	        	rbo.setChainSkuId(bo.getChainSkuId());
	            List<ChainGoodsSkuItemsDetailVO> childrens = new ArrayList<ChainGoodsSkuItemsDetailVO>();
	            rbo.setChildrens(childrens);
	            map.put(bo.getChainSkuId(),rbo);
	        }
	        
	        ChainGoodsSkuItemsDetailVO chainGoodsSkuItemsDetailVO=new ChainGoodsSkuItemsDetailVO();
	        chainGoodsSkuItemsDetailVO.setChainSkuItemsId(bo.getChainSkuItemsId());
	        chainGoodsSkuItemsDetailVO.setStandard(bo.getStandard());
	        rbo.getChildrens().add(chainGoodsSkuItemsDetailVO);
	    }
		Collection<ChainGoodsSkuItemsVO> data = map.values();
		result.setData(data);
		return result;
	}
	
	
	@Override
	public ResultVO querySkuContent(String chainGoodsId) {
		ResultVO result = new ResultVO();
		List<ChainSkuContent> data = chainSkuContentDAO.querySkuContent(chainGoodsId);
		result.setData(data);
		return result;
	}

	@Transactional
	@Override
	public ResultVO addGoodsSku(List<ChainSkuContent> chainSkuContentlist, List<ChainGoodsSkuItems> chainGoodsSkuItemlist, String chainGoodsId) {
		ResultVO result = new ResultVO();
		
		chainSkuContentDAO.deleteSkuContent(chainGoodsId);
		chainGoodsSkuItemsDAO.deleteGoodsSkuItems(chainGoodsId);
		chainSkuContentDAO.batchInsert(chainSkuContentlist);
		chainGoodsSkuItemsDAO.batchInsert(chainGoodsSkuItemlist);
		
		//购物规格失效
		cartDAO.updateStatus(chainGoodsId, Status.INACTIVE.toIntStatus());
		return result;
	}

}
